package com.wechat.controller;

import com.wechat.annotation.GlobalInterceptor;
import com.wechat.entity.dto.TokenUserInfoDto;
import com.wechat.entity.dto.UserContactSearchResultDto;
import com.wechat.entity.enums.PageSize;
import com.wechat.entity.enums.ResponseCodeEnum;
import com.wechat.entity.enums.UserContactStatusEnum;
import com.wechat.entity.enums.UserContactTypeEnum;
import com.wechat.entity.po.UserContact;
import com.wechat.entity.po.UserContactApply;
import com.wechat.entity.po.UserInfo;
import com.wechat.entity.query.UserContactApplyQuery;
import com.wechat.entity.query.UserContactQuery;
import com.wechat.entity.vo.PaginationResultVO;
import com.wechat.entity.vo.ResponseVO;
import com.wechat.entity.vo.UserInfoVO;
import com.wechat.exception.BusinessException;
import com.wechat.service.UserContactApplyService;
import com.wechat.service.UserContactService;
import com.wechat.service.UserInfoService;
import jodd.util.ArraysUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@RequestMapping("/contact")
public class UserContactController extends ABaseController {
    @Autowired
    private UserContactService userContactService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserContactApplyService userContactApplyService;

    /**
     * 搜索联系人---根据ID搜索
     *
     * @param request
     * @param contactId
     * @return
     */
    @RequestMapping("/search")
    @GlobalInterceptor
    public ResponseVO searchContact(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        String userId = tokenUserInfo.getUserId();
        UserContactSearchResultDto ResultDto = userContactService.searchContact(userId, contactId);
        return getSuccessResponseVO(ResultDto);
    }

    /**
     * 添加联系人
     *
     * @param request
     * @param contactId
     * @param applyInfo
     * @return
     */
    @RequestMapping("/applyAdd")
    @GlobalInterceptor
    public ResponseVO applyAdd(HttpServletRequest request, @NotEmpty String contactId, String applyInfo) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        Integer joinType = userContactApplyService.applyAdd(tokenUserInfo, contactId, applyInfo);
        return getSuccessResponseVO(joinType);
    }

    /**
     * 用户加载申请列表
     *
     * @param request
     * @param pageNum
     * @return
     */
    @RequestMapping("/loadApply")
    @GlobalInterceptor
    public ResponseVO loadApply(HttpServletRequest request, Integer pageNum) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        String userId = tokenUserInfo.getUserId();

        UserContactApplyQuery applyQuery = new UserContactApplyQuery();
        applyQuery.setOrderBy("last_apply_time desc");//按申请时间倒序
        applyQuery.setReceiveUserId(userId);
        applyQuery.setPageNo(pageNum);//当前页码
        applyQuery.setPageSize(PageSize.SIZE15.getSize());//每页15条数据

        applyQuery.setQueryContactInfo(true);//设置是否设置关联查询联系人信息

        PaginationResultVO resultVO = userContactApplyService.findListByPage(applyQuery);
        return getSuccessResponseVO(resultVO);
    }

    /**
     * 处理申请
     *
     * @param request
     * @param applyId
     * @param status---1:拒绝，2：同意 3：拉黑
     * @return
     */
    @RequestMapping("/delWithApply")
    @GlobalInterceptor
    public ResponseVO delWithApply(HttpServletRequest request, Integer applyId, Integer status) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        userContactApplyService.dealWithApply(tokenUserInfoDto.getUserId(), applyId, status);
        return getSuccessResponseVO(null);

    }

    /**
     * 加载联系人
     *
     * @param request
     * @param contactType
     * @return
     */
    @RequestMapping("/loadContact")
    @GlobalInterceptor
    public ResponseVO loadContact(HttpServletRequest request,@NotEmpty String contactType) {
        UserContactTypeEnum typeEnum = UserContactTypeEnum.getByName(contactType);
        if (typeEnum == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        //封装查询条件
        UserContactQuery contactQuery = UserContactQuery.builder()
                .userId(tokenUserInfoDto.getUserId())
                .contactType(typeEnum.getType())
                .build();
        //分类型加载并关联查询
        if(UserContactTypeEnum.USER.getType().equals(typeEnum.getType())){
            contactQuery.setQueryContactUserInfo(true);//关联好友
        }else if(UserContactTypeEnum.GROUP.getType().equals(typeEnum.getType())){
            contactQuery.setQueryGroupInfo(true);//关联群组
            contactQuery.setExcludeMyGroup(true);
        }
        contactQuery.setOrderBy("last_update_time des");
        contactQuery.setStatusArray(
                new Integer[]{
                        UserContactStatusEnum.FRIEND.getStatus(),
                        UserContactStatusEnum.DEL_BE.getStatus(),
                        UserContactStatusEnum.BLACKLIST_BE.getStatus()
                }
        );
        List<UserContact> contactList = userContactService.findListByParam(contactQuery);
        return getSuccessResponseVO(contactList);
    }

    /**
     * 获取用户信息---联系人+非联系人
     *
     * @param request
     * @param contactId
     * @return
     */
    @RequestMapping("/getContactInfo")
    @GlobalInterceptor
    public ResponseVO getContactInfo(HttpServletRequest request,@NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        String userId = tokenUserInfoDto.getUserId();
        //获取联系人信息---好友/群聊内的联系人信息
        UserInfo userInfo=userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO userContactInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userContactInfoVO);
        userContactInfoVO.setContactStatus(UserContactStatusEnum.NOT_FRIEND.getStatus());
        //判断是否是好友
        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(userId, contactId);
        if (userContact != null) {
            userContactInfoVO.setContactStatus(UserContactStatusEnum.FRIEND.getStatus());
        }
        return getSuccessResponseVO(userContactInfoVO);

    }


    /**
     * 获取好友(列表中联系人)信息
     *
     * @param request
     * @param contactId
     * @return
     */
    @RequestMapping("/getContactUserInfo")
    @GlobalInterceptor
    public ResponseVO getContactUserInfo(HttpServletRequest request,@NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        String userId = tokenUserInfoDto.getUserId();
        //判断是否是好友
        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(userId, contactId);
        if (userContact == null || !ArraysUtil.contains(new Integer[]{
                UserContactStatusEnum.FRIEND.getStatus(),
                UserContactStatusEnum.DEL_BE.getStatus(),
                UserContactStatusEnum.BLACKLIST_BE.getStatus()
        }, userContact.getStatus())) {
            throw new BusinessException("您还不是该联系人");
        }
        //获取联系人信息---好友/群聊内的联系人信息
        UserInfo userInfo=userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO userContactInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userContactInfoVO);
        userContactInfoVO.setContactStatus(UserContactStatusEnum.NOT_FRIEND.getStatus());

        return getSuccessResponseVO(userContactInfoVO);

    }


    /**
     * 删除联系人
     *
     * @param request
     * @param contactId
     * @return
     */
    @RequestMapping("/delContact")
    @GlobalInterceptor
    public ResponseVO delContact(HttpServletRequest request,@NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        String userId = tokenUserInfoDto.getUserId();
        userContactService.removeUserContact(userId, contactId, UserContactStatusEnum.DEL);
        return getSuccessResponseVO(null);

    }

    /**
     * 添加联系人到黑名单
     *
     * @param request
     * @param contactId
     * @return
     */
    @RequestMapping("/addContact2BlackList")
    @GlobalInterceptor
    public ResponseVO addContact2BlackList(HttpServletRequest request,@NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        String userId = tokenUserInfoDto.getUserId();
        userContactService.removeUserContact(userId, contactId, UserContactStatusEnum.BLACKLIST);
        return getSuccessResponseVO(null);

    }






}
